Skip to main content

Overview

Single-node training is ideal for experiments and medium-scale training on one machine with multiple GPUs. OpenCLIP uses torchrun (PyTorch’s distributed launcher) for efficient multi-GPU training on a single node.

Prerequisites

  • Single machine with 1 or more GPUs
  • CUDA-capable GPUs (recommended: V100, A100, or newer)
  • OpenCLIP installed with training dependencies
  • Training data prepared in CSV or WebDataset format

Basic Single-Node Training

Single GPU Training

For single GPU training, you can use the training script directly without torchrun:
The --imagenet-val argument should point to the validation set of ImageNet for zero-shot evaluation, not the training set. The val folder should contain subfolders for each class.

Multi-GPU Training with torchrun

For training on multiple GPUs on a single node, use torchrun with the --nproc_per_node flag:
Key Parameters:
  • --nproc_per_node 4: Number of GPUs to use (4 GPUs in this example)
  • --batch-size 320: Per-GPU batch size (total batch size = 320 × 4 = 1280)
  • --workers 4: Number of data loading workers per GPU
  • --precision amp: Automatic Mixed Precision for faster training and lower memory usage

WebDataset Training Example

WebDataset format is recommended for datasets larger than 10M samples:
WebDataset-specific flags:
  • --dataset-type webdataset: Specify WebDataset format
  • --dataset-resampled: Enable sampling with replacement (recommended for large datasets)
  • --train-num-samples: Total number of samples in dataset

Batch Size and Worker Configuration

Calculating Effective Batch Size

The effective batch size is:
Example:
  • --batch-size 256 (per GPU)
  • --nproc_per_node 4 (4 GPUs)
  • --accum-freq 1 (no gradient accumulation)
  • Effective batch size: 256 × 4 × 1 = 1024

Optimizing Worker Count

The --workers parameter controls the number of data loading processes per GPU:
Guidelines:
  • Start with 4-8 workers per GPU
  • Too few workers: GPU starvation (low utilization)
  • Too many workers: CPU/memory overhead
  • Monitor GPU utilization and adjust accordingly

Memory Optimization

If you run out of GPU memory, try these options in order:
  1. Enable Mixed Precision:
  2. Reduce Batch Size:
  3. Enable Gradient Checkpointing:
  4. Use Gradient Accumulation:

Monitoring Training

TensorBoard

Launch TensorBoard to monitor training progress:
Training command with TensorBoard:

Weights & Biases (wandb)

For cloud-based experiment tracking:

Both TensorBoard and wandb

You can log to both simultaneously:

Zero-Shot Evaluation During Training

Automatic zero-shot evaluation on ImageNet during training:
Parameters:
  • --imagenet-val: Path to ImageNet validation set
  • --zeroshot-frequency 1: Run zero-shot eval every epoch
  • --zeroshot-frequency 2: Run zero-shot eval every 2 epochs

Complete Training Example

Here’s a complete example training ViT-B/32 on CC12M with 4 GPUs:

Advanced Configuration

Custom Learning Rate Schedule

Patch Dropout for ViT Models

Speed up Vision Transformer training by 2-3x:

Gradient Clipping

Prevent gradient explosion:

Model-Specific Optimizations

For Vision Transformers (ViT):
For ResNet Models:

Checkpointing and Resuming

Automatic Checkpointing

Checkpoints are saved automatically:

Resume Training

Resume from a specific checkpoint:
Resume from latest checkpoint:

Save Most Recent Checkpoint Only

To save disk space, keep only the latest checkpoint:

Performance Optimization

GPU Utilization

Monitor GPU usage:
Target: 90%+ GPU utilization If GPU utilization is low:
  • Increase --workers (data loading parallelism)
  • Use faster storage (NVMe SSD)
  • Increase --batch-size if memory allows
  • Ensure data is preprocessed and ready

Training Speed

Typical training speeds on A100 GPUs:

Troubleshooting

Out of Memory Errors

Solutions:
  1. Reduce --batch-size
  2. Enable --precision amp
  3. Use --grad-checkpointing
  4. Increase --accum-freq and reduce --batch-size

Data Loading Bottleneck

Solutions:
  1. Increase --workers
  2. Use faster storage (SSD vs HDD)
  3. Preprocess data to WebDataset format
  4. Check network speed if data is remote

Port Already in Use

Solution:

ImageNet Validation Issues

If zero-shot evaluation fails, ensure:
  1. --imagenet-val points to validation set (not training set)
  2. Directory structure is correct:
  3. Use the ImageNet validation prep script if needed

Example Training Scripts

Small-Scale Experiment (RN50 on CC3M)

Medium-Scale (ViT-B/32 on CC12M)

Large Model (ViT-L/14)

Next Steps

Multi-Node Training

Scale to multiple machines with torchrun or SLURM

Configuration

Explore all available training parameters

Distributed Training

Advanced distributed training techniques

Data Preparation

Prepare datasets in CSV or WebDataset format